// pages/nearsearch/nearsearch.js

// 引入SDK核心类

/**/
var tab = require('../../libs/map-tab.js');
var amapFile = require('../../libs/amap-wx.js');
let amap = require("../../libs/amap.js");

var myAmapFun = new amapFile.AMapWX({

  key: '1336bfc7fd9f11ec452d1d4f0cab4c8c' // 必填 换成自己申请到的

});


Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    placeHould: '查找美食、景区、酒店、超市、娱乐场所',
    jd: null,
    wd: null,
    tabs: [],
    currentTab: '',
    markers: [],
    points: [],
    textData: {},
    map_h:'calc(100vh - 105rpx - 80rpx)',
    select_place:{},
    city:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    const tabs = tab.getTabs('near');
    this.setData({
      tabs: tabs,
      currentTab: tabs[0].name
    })

    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        console.log(res.latitude, res.longitude, '当前位置经纬度')
        var latitude1 = res.latitude;
        var longitude1 = res.longitude;
        var mapcenter = [latitude1, longitude1];

        that.setData({
          wd: latitude1,
          jd: longitude1,
          points: [{
            longitude: res.longitude,
            latitude: res.latitude
          }],

        })
      }
    })

  },
  makertap: function(e) {
    console.log(e.markerId, e, '点击气泡');
    var that = this;

    var id = e.markerId;
    var markersData = that.data.markers;
    that.showMarkerInfo(markersData, id);

    that.setData({
      map_h:'calc(100vh - 105rpx - 80rpx - 80px)'
    })
  },
  showMarkerInfo: function(data, id) {

    var that = this;

    data.map((track) => {
      if (track.id == id) {
        console.log(track);

        that.setData({
          select_place: track,
          textData: {
            name: track.name,
            desc: track.address
          }
        });
      }
    })

  },
  clickcontrol: function(e) {

  },
  mapchange: function(e) {
    console.log(e, 'mapchange拖拽地图触发')
  },
  suo_result: function() {
    console.log('搜索结果');
    
    let { wd, jd, city } = this.data;
    wx.navigateTo({
      url: `../storesearch/storesearch?city=${city}&lonlat=${jd},${wd}`,
    })

  },
  changeTab(e) {
    if (e.currentTarget.dataset.tab !== this.data.currentTab){
      this.setData({
        currentTab: e.currentTarget.dataset.tab
      })
    }
  },
  goDetail: function () {
    var that = this;
    wx.openLocation({
      latitude: parseFloat(that.data.select_place.latitude),
      longitude: parseFloat(that.data.select_place.longitude),
      name: that.data.select_place.name,
      scale: 28
    })
  },
  onfocus:function(){
    
  },
  onBindChange:function(e){
    var that = this;
    
    console.log(e.detail.value);
    var v = e.detail.value;

    that.getMarksLocation(v);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
/**/
    that.getMarksLocation();

  },
  getMarksLocation:function(keywords){
    var that = this;

    var k = keywords || '';
    myAmapFun.getPoiAround({
      querykeywords: k,
      success: function (data) {
        //成功回调
        console.log(data, 'success');

        var markers = data.markers;
        var poisData = data.poisData;
        var city = poisData[0].pname + poisData[0].cityname;
        var mark = [];

        for (var i = 0; i < markers.length; i++) {
          mark[i] = {
            iconPath: "../../images/position.png",
            id: poisData[i].id,
            latitude: markers[i].latitude,
            longitude: markers[i].longitude,
            width: 20,
            height: 20,
            name: markers[i].name,
            address: markers[i].address
          }
        }
        that.setData({
          markers: mark,
          city: city
        })

      },
      fail: function (info) {
        //失败回调
        console.log(info, 'fail');
        wx.showToast({
          title: info.errMsg
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})

// https://blog.csdn.net/eadio/article/details/78920196